﻿package org.flixel
{

    public class FlxGroup extends FlxBasic
    {
        public var members:Array;
        public var length:Number;
        protected var _maxSize:uint;
        protected var _marker:uint;
        protected var _sortIndex:String;
        protected var _sortOrder:int;
        public static const ASCENDING:int = -1;
        public static const DESCENDING:int = 1;

        public function FlxGroup(param1:uint = 0)
        {
            this.members = new Array();
            this.length = 0;
            this._maxSize = param1;
            this._marker = 0;
            this._sortIndex = null;
            return;
        }// end function

        override public function destroy() : void
        {
            var _loc_1:FlxBasic = null;
            var _loc_2:uint = 0;
            if (this.members != null)
            {
                _loc_2 = 0;
                while (_loc_2 < this.length)
                {
                    
                    _loc_1 = this.members[_loc_2++] as FlxBasic;
                    if (_loc_1 != null)
                    {
                        _loc_1.destroy();
                    }
                }
                this.members.length = 0;
                this.members = null;
            }
            this._sortIndex = null;
            return;
        }// end function

        override public function preUpdate() : void
        {
            return;
        }// end function

        override public function update() : void
        {
            var _loc_1:FlxBasic = null;
            var _loc_2:uint = 0;
            while (_loc_2 < this.length)
            {
                
                _loc_1 = this.members[_loc_2++] as FlxBasic;
                if (_loc_1 != null && _loc_1.exists && _loc_1.active)
                {
                    _loc_1.preUpdate();
                    _loc_1.update();
                    _loc_1.postUpdate();
                }
            }
            return;
        }// end function

        override public function draw() : void
        {
            var _loc_1:FlxBasic = null;
            var _loc_2:uint = 0;
            while (_loc_2 < this.length)
            {
                
                _loc_1 = this.members[_loc_2++] as FlxBasic;
                if (_loc_1 != null && _loc_1.exists && _loc_1.visible)
                {
                    _loc_1.draw();
                }
            }
            return;
        }// end function

        public function get maxSize() : uint
        {
            return this._maxSize;
        }// end function

        public function set maxSize(param1:uint) : void
        {
            var _loc_2:FlxBasic = null;
            this._maxSize = param1;
            if (this._marker >= this._maxSize)
            {
                this._marker = 0;
            }
            if (this._maxSize == 0 || this.members == null || this._maxSize >= this.members.length)
            {
                return;
            }
            var _loc_3:* = this._maxSize;
            var _loc_4:* = this.members.length;
            while (_loc_3 < _loc_4)
            {
                
                _loc_2 = this.members[_loc_3++] as FlxBasic;
                if (_loc_2 != null)
                {
                    _loc_2.destroy();
                }
            }
            var _loc_5:* = this._maxSize;
            this.members.length = this._maxSize;
            this.length = _loc_5;
            return;
        }// end function

        public function add(param1:FlxBasic) : FlxBasic
        {
            if (this.members.indexOf(param1) >= 0)
            {
                return param1;
            }
            var _loc_2:uint = 0;
            var _loc_3:* = this.members.length;
            while (_loc_2 < _loc_3)
            {
                
                if (this.members[_loc_2] == null)
                {
                    this.members[_loc_2] = param1;
                    if (_loc_2 >= this.length)
                    {
                        this.length = _loc_2 + 1;
                    }
                    return param1;
                }
                _loc_2 = _loc_2 + 1;
            }
            if (this._maxSize > 0)
            {
                if (this.members.length >= this._maxSize)
                {
                    return param1;
                }
                if (this.members.length * 2 <= this._maxSize)
                {
                    this.members.length = this.members.length * 2;
                }
                else
                {
                    this.members.length = this._maxSize;
                }
            }
            else
            {
                this.members.length = this.members.length * 2;
            }
            this.members[_loc_2] = param1;
            this.length = _loc_2 + 1;
            return param1;
        }// end function

        public function recycle(param1:Class = null) : FlxBasic
        {
            var _loc_2:FlxBasic = null;
            if (this._maxSize > 0)
            {
                if (this.length < this._maxSize)
                {
                    if (param1 == null)
                    {
                        return null;
                    }
                    return this.add(new param1 as FlxBasic);
                }
                else
                {
                    var _loc_3:String = this;
                    _loc_3._marker = this._marker + 1;
                    _loc_2 = this.members[this._marker++];
                    if (this._marker >= this._maxSize)
                    {
                        this._marker = 0;
                    }
                    return _loc_2;
                }
            }
            else
            {
                _loc_2 = this.getFirstAvailable(param1);
                if (_loc_2 != null)
                {
                    return _loc_2;
                }
                if (param1 == null)
                {
                    return null;
                }
                return this.add(new param1 as FlxBasic);
            }
        }// end function

        public function remove(param1:FlxBasic, param2:Boolean = false) : FlxBasic
        {
            var _loc_3:* = this.members.indexOf(param1);
            if (_loc_3 < 0 || _loc_3 >= this.members.length)
            {
                return null;
            }
            if (param2)
            {
                this.members.splice(_loc_3, 1);
                var _loc_4:String = this;
                var _loc_5:* = this.length - 1;
                _loc_4.length = _loc_5;
            }
            else
            {
                this.members[_loc_3] = null;
            }
            return param1;
        }// end function

        public function replace(param1:FlxBasic, param2:FlxBasic) : FlxBasic
        {
            var _loc_3:* = this.members.indexOf(param1);
            if (_loc_3 < 0 || _loc_3 >= this.members.length)
            {
                return null;
            }
            this.members[_loc_3] = param2;
            return param2;
        }// end function

        public function sort(param1:String = "y", param2:int = -1) : void
        {
            this._sortIndex = param1;
            this._sortOrder = param2;
            this.members.sort(this.sortHandler);
            return;
        }// end function

        public function setAll(param1:String, param2:Object, param3:Boolean = true) : void
        {
            var _loc_4:FlxBasic = null;
            var _loc_5:uint = 0;
            while (_loc_5 < this.length)
            {
                
                _loc_4 = this.members[_loc_5++] as FlxBasic;
                if (_loc_4 != null)
                {
                    if (param3 && _loc_4 is FlxGroup)
                    {
                        (_loc_4 as FlxGroup).setAll(param1, param2, param3);
                        continue;
                    }
                    _loc_4[param1] = param2;
                }
            }
            return;
        }// end function

        public function callAll(param1:String, param2:Boolean = true) : void
        {
            var _loc_3:FlxBasic = null;
            var _loc_4:uint = 0;
            while (_loc_4 < this.length)
            {
                
                _loc_3 = this.members[_loc_4++] as FlxBasic;
                if (_loc_3 != null)
                {
                    if (param2 && _loc_3 is FlxGroup)
                    {
                        (_loc_3 as FlxGroup).callAll(param1, param2);
                        continue;
                    }
                    var _loc_5:* = _loc_3;
                    _loc_5._loc_3[param1]();
                }
            }
            return;
        }// end function

        public function getFirstAvailable(param1:Class = null) : FlxBasic
        {
            var _loc_2:FlxBasic = null;
            var _loc_3:uint = 0;
            while (_loc_3 < this.length)
            {
                
                _loc_2 = this.members[_loc_3++] as FlxBasic;
                if (_loc_2 != null && !_loc_2.exists && (param1 == null || _loc_2 is param1))
                {
                    return _loc_2;
                }
            }
            return null;
        }// end function

        public function getFirstNull() : int
        {
            var _loc_1:FlxBasic = null;
            var _loc_2:uint = 0;
            var _loc_3:* = this.members.length;
            while (_loc_2 < _loc_3)
            {
                
                if (this.members[_loc_2] == null)
                {
                    return _loc_2;
                }
                _loc_2 = _loc_2 + 1;
            }
            return -1;
        }// end function

        public function getFirstExtant() : FlxBasic
        {
            var _loc_1:FlxBasic = null;
            var _loc_2:uint = 0;
            while (_loc_2 < this.length)
            {
                
                _loc_1 = this.members[_loc_2++] as FlxBasic;
                if (_loc_1 != null && _loc_1.exists)
                {
                    return _loc_1;
                }
            }
            return null;
        }// end function

        public function getFirstAlive() : FlxBasic
        {
            var _loc_1:FlxBasic = null;
            var _loc_2:uint = 0;
            while (_loc_2 < this.length)
            {
                
                _loc_1 = this.members[_loc_2++] as FlxBasic;
                if (_loc_1 != null && _loc_1.exists && _loc_1.alive)
                {
                    return _loc_1;
                }
            }
            return null;
        }// end function

        public function getFirstDead() : FlxBasic
        {
            var _loc_1:FlxBasic = null;
            var _loc_2:uint = 0;
            while (_loc_2 < this.length)
            {
                
                _loc_1 = this.members[_loc_2++] as FlxBasic;
                if (_loc_1 != null && !_loc_1.alive)
                {
                    return _loc_1;
                }
            }
            return null;
        }// end function

        public function countLiving() : int
        {
            var _loc_2:FlxBasic = null;
            var _loc_1:int = -1;
            var _loc_3:uint = 0;
            while (_loc_3 < this.length)
            {
                
                _loc_2 = this.members[_loc_3++] as FlxBasic;
                if (_loc_2 != null)
                {
                    if (_loc_1 < 0)
                    {
                        _loc_1 = 0;
                    }
                    if (_loc_2.exists && _loc_2.alive)
                    {
                        _loc_1++;
                    }
                }
            }
            return _loc_1;
        }// end function

        public function countDead() : int
        {
            var _loc_2:FlxBasic = null;
            var _loc_1:int = -1;
            var _loc_3:uint = 0;
            while (_loc_3 < this.length)
            {
                
                _loc_2 = this.members[_loc_3++] as FlxBasic;
                if (_loc_2 != null)
                {
                    if (_loc_1 < 0)
                    {
                        _loc_1 = 0;
                    }
                    if (!_loc_2.alive)
                    {
                        _loc_1++;
                    }
                }
            }
            return _loc_1;
        }// end function

        public function getRandom(param1:uint = 0, param2:uint = 0) : FlxBasic
        {
            if (param2 == 0)
            {
                param2 = this.length;
            }
            return FlxG.getRandom(this.members, param1, param2) as FlxBasic;
        }// end function

        public function clear() : void
        {
            var _loc_1:int = 0;
            this.members.length = 0;
            this.length = _loc_1;
            return;
        }// end function

        override public function kill() : void
        {
            var _loc_1:FlxBasic = null;
            var _loc_2:uint = 0;
            while (_loc_2 < this.length)
            {
                
                _loc_1 = this.members[_loc_2++] as FlxBasic;
                if (_loc_1 != null && _loc_1.exists)
                {
                    _loc_1.kill();
                }
            }
            super.kill();
            return;
        }// end function

        protected function sortHandler(param1:FlxBasic, param2:FlxBasic) : int
        {
            if (param1[this._sortIndex] < param2[this._sortIndex])
            {
                return this._sortOrder;
            }
            if (param1[this._sortIndex] > param2[this._sortIndex])
            {
                return -this._sortOrder;
            }
            return 0;
        }// end function

    }
}
